Added geojson read capablity, moved magic strings to constants, fixed windows compila...
authorHarel Mazor <harel.mazor@gmail.com>
Mon, 23 Jan 2017 22:35:04 +0000 (00:35 +0200)
committerHarel Mazor <harel.mazor@gmail.com>
Mon, 23 Jan 2017 22:35:04 +0000 (00:35 +0200)
GPSBabel.pro
geojson.cc
mtk_logger.cc
tef_xml.cc

index 4b8e378a14c80ce95c9c8d6a50ee2f004e337c78..1c06c4abcf9c9061372a072abbb49f5494e4375e 100644 (file)
@@ -29,7 +29,7 @@ ALL_FMTS=$$MINIMAL_FMTS gtm.cc gpsutil.cc pcx.cc \
         vpl.cc teletype.cc jogmap.cc bushnell.cc bushnell_trl.cc wintec_tes.cc \
         subrip.cc garmin_xt.cc garmin_fit.cc lowranceusr4.cc \
         mtk_locus.cc googledir.cc mapbar_track.cc mapfactor.cc f90g_track.cc \
-        energympro.cc mynav.cc ggv_bin.cc globalsat_sport.cc
+        energympro.cc mynav.cc ggv_bin.cc globalsat_sport.cc geojson.cc
 
 DEPRECIATED_FMTS=cetus.cc copilot.cc gpspilot.cc magnav.cc psp.cc gcdb.cc quovadis.cc gpilots.cc geoniche.cc palmdoc.cc hsa_ndv.cc coastexp.cc pathaway.cc coto.cc msroute.cc mag_pdb.cc axim_gpb.cc delbin.cc google.cc
 
@@ -148,7 +148,7 @@ win32 {
   }
   SOURCES += gbser_win.cc
   JEEPS += jeeps/gpsusbwin.cc
-  LIBS += "C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/setupapi.lib" "C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/hid.lib"
+  LIBS += "C:/Program Files (x86)/Windows Kits/8.1/lib/winv6.3/um/x64/setupapi.lib" "C:/Program Files (x86)/Windows Kits/8.1/lib/winv6.3/um/x64/hid.lib"
 }
 
 win32-msvc*{
index 085ae0fad0fd8ef09c4b062f42862b0dfce08fbd..00bb41bb7f76e4fcce45dbe545cd6981c43bde36 100644 (file)
 #include "src/core/file.h"
 
 static gbfile* ofd;
+static QString input_file_name;
 static const char MYNAME[] = "geojson";
 static char* compact_opt = NULL;
 static QJsonObject* track_object = NULL;
 static QJsonArray* track_coords = NULL;
 
+static const QString FEATURE_COLLECTION = QStringLiteral("FeatureCollection");
+static const QString FEATURE = QStringLiteral("Feature");
+static const QString POINT = QStringLiteral("Point");
+static const QString MULTIPOINT = QStringLiteral("MultiPoint");
+static const QString LINESTRING = QStringLiteral("LineString");
+static const QString MULTILINESTRING = QStringLiteral("MultiLineString");
+static const QString POLYGON = QStringLiteral("Polygon");
+static const QString MULTIPOLYGON = QStringLiteral("MultiPolygon");
+static const QString TYPE = QStringLiteral("type");
+static const QString FEATURES = QStringLiteral("features");
+static const QString COORDINATES = QStringLiteral("coordinates");
+static const QString GEOMETRY = QStringLiteral("geometry");
+static const QString PROPERTIES = QStringLiteral("properties");
+static const QString NAME = QStringLiteral("name");
+static const QString DESCRIPTION = QStringLiteral("description");
+static const QString URL = QStringLiteral("url");
+static const QString URLNAME = QStringLiteral("urlname");
+
 static arglist_t geojson_args[] = {
   {"compact", &compact_opt, "Compact Output. Default is off.", 
     NULL, ARGTYPE_BOOL, ARG_NOMINMAX } ,
@@ -36,6 +55,7 @@ static arglist_t geojson_args[] = {
 
 static void
 geojson_rd_init(const QString& fname) {
+       input_file_name = fname;
 }
 
 QJsonArray* feature_collection = nullptr;
@@ -48,57 +68,55 @@ geojson_wr_init(const QString& fname) {
 
 static void
 geojson_waypt_pr(const Waypoint* waypoint) {
-  QJsonObject object;
-  static const QString kType = QStringLiteral("type");
-  object[kType] = QStringLiteral("Feature");
-  
   QJsonObject geometry;
-  geometry[kType] = QStringLiteral("Point");
-
-  QJsonArray coords;
-  coords.append(waypoint->longitude);
-  coords.append(waypoint->latitude);
+  geometry[TYPE] = POINT;
+  QJsonArray coordinates;
+  coordinates.append(waypoint->longitude);
+  coordinates.append(waypoint->latitude);
   if (waypoint->altitude != unknown_alt && waypoint->altitude != 0) {
-    coords.append(waypoint->altitude);
+    coordinates.append(waypoint->altitude);
   }
+  geometry[COORDINATES] = coordinates;
 
-  geometry[kType] = QStringLiteral("Point");
-  geometry[QStringLiteral("coordinates")] = coords;
-  object[QStringLiteral("geometry")] = geometry;
+  QJsonObject feature;
+  feature[TYPE] = FEATURE;
+  feature[GEOMETRY] = geometry;
 
   // Build up the properties.
   QJsonObject properties;
   if (!waypoint->shortname.isEmpty()) {
-    properties["name"] = waypoint->shortname;
+    properties[NAME] = waypoint->shortname;
   }
   if (!waypoint->description.isEmpty()) {
-    properties["description"] = waypoint->description;
+    properties[DESCRIPTION] = waypoint->description;
   }
   if (waypoint->HasUrlLink()) {
     UrlLink link = waypoint->GetUrlLink();
     if (!link.url_.isEmpty()) {
-      properties["url"] = link.url_;
+      properties[URL] = link.url_;
     }
     if (!link.url_link_text_.isEmpty()) {
-      properties["urlname"] = link.url_link_text_;
+      properties[URLNAME] = link.url_link_text_;
     }
   }
   if (!properties.empty()) {
-    object["properties"] = properties;
+    feature[PROPERTIES] = properties;
   }
   
-  feature_collection->append(object);
+  feature_collection->append(feature);
 }
 
 static void
 geojson_rd_deinit() {
+       gbfclose(ofd);
+       ofd = NULL;
 }
 
 static void
 geojson_wr_deinit(void) {
   QJsonObject object;
-  object[QStringLiteral("type")] = QStringLiteral("FeatureCollection");
-  object[QStringLiteral("features")]  = *feature_collection;
+  object[TYPE] = FEATURE_COLLECTION;
+  object[FEATURES]  = *feature_collection;
 
   QJsonDocument save(object);
   QJsonDocument::JsonFormat style;
@@ -111,21 +129,157 @@ geojson_wr_deinit(void) {
   feature_collection = nullptr;
 }
 
+static Waypoint* 
+waypoint_from_coordinates(const QJsonArray& coordinates)
+{
+       auto waypoint = new Waypoint();
+       waypoint->latitude = coordinates.at(1).toDouble();
+       waypoint->longitude = coordinates.at(0).toDouble();
+       if (coordinates.size() > 2)
+       {
+               waypoint->altitude = coordinates.at(3).toDouble();
+       }
+       return waypoint;
+}
+
+static void 
+routes_from_polygon_coordinates(const QJsonArray& polygon)
+{
+       for (auto lineStringIterator = polygon.begin(); lineStringIterator != polygon.end(); ++lineStringIterator)
+       {
+               QJsonArray coordinates = (*lineStringIterator).toArray();
+               auto route = route_head_alloc();
+               route_add_head(route);
+               for (auto coordinates_iterator = coordinates.begin(); coordinates_iterator != coordinates.end(); ++coordinates_iterator)
+               {
+                       auto waypoint = waypoint_from_coordinates((*coordinates_iterator).toArray());
+                       route_add_wpt(route, waypoint);
+               }
+       }
+}
+
 static void
 geojson_read(void) {
+       QFile file;
+       file.setFileName(input_file_name);
+       file.open(QIODevice::ReadOnly | QIODevice::Text);
+       QString file_content = file.readAll();
+       file.close();
+       QJsonParseError error;
+       QJsonDocument document = QJsonDocument::fromJson(file_content.toUtf8(), &error);
+       QJsonObject rootObject = document.object();
+
+       if (rootObject[TYPE] != FEATURE_COLLECTION)
+       {
+               return;
+       }
+       QJsonArray features = rootObject.value(FEATURES).toArray();
+       for (auto iterator = features.begin(); iterator != features.end(); ++iterator)
+       {
+               QJsonObject feature = (*iterator).toObject();
+               QJsonObject properties = (feature.value(PROPERTIES)).toObject();
+               QString name;
+               QString description;
+               if (!properties.empty())
+               {
+                       if (properties.contains(NAME))
+                       {
+                               name = properties[NAME].toString();
+                       }
+                       if (properties.contains(DESCRIPTION))
+                       {
+                               description = properties[DESCRIPTION].toString();
+                       }
+               }
+               
+               QJsonObject geometry = feature.value(GEOMETRY).toObject();
+               auto geometry_type = geometry[TYPE];
+               if (geometry_type == POINT)
+               {
+                       QJsonArray coordinates = geometry.value(COORDINATES).toArray();
+                       auto waypoint = waypoint_from_coordinates(coordinates);
+                       waypoint->shortname = name;
+                       waypoint->description = description;
+                       if (properties.contains(URL))
+                       {
+                               QString url = properties[URL].toString();
+                               if (properties.contains(URLNAME))
+                               {
+                                       QString url_text = properties[URLNAME].toString();
+                                       waypoint->AddUrlLink(UrlLink(url, url_text));
+                               }
+                               else
+                               {
+                                       waypoint->AddUrlLink(UrlLink(url));
+                               }
+                       }
+                       waypt_add(waypoint);
+               }
+               else if (geometry_type == MULTIPOINT)
+               {
+                       QJsonArray coordinates = geometry.value(COORDINATES).toArray();
+                       for (auto coordinates_iterator = coordinates.begin(); coordinates_iterator != coordinates.end(); ++coordinates_iterator)
+                       {
+                               auto waypoint = waypoint_from_coordinates((*coordinates_iterator).toArray());
+                               waypt_add(waypoint);
+                       }
+               }
+               else if (geometry_type == LINESTRING)
+               {
+                       QJsonArray coordinates = geometry.value(COORDINATES).toArray();
+                       auto route = route_head_alloc();
+                       route->rte_name = name;
+                       route_add_head(route);
+                       for (auto coordinates_iterator = coordinates.begin(); coordinates_iterator != coordinates.end(); ++coordinates_iterator)
+                       {
+                               auto waypoint = waypoint_from_coordinates((*coordinates_iterator).toArray());
+                               route_add_wpt(route, waypoint);
+                       }
+               }
+               else if (geometry_type == POLYGON)
+               {
+                       QJsonArray polygon = geometry.value(COORDINATES).toArray();
+                       routes_from_polygon_coordinates(polygon);
+               }
+               else if (geometry_type == MULTIPOLYGON)
+               {
+                       QJsonArray polygons = geometry.value(COORDINATES).toArray();
+                       for (auto polygons_iterator = polygons.begin(); polygons_iterator != polygons.end(); ++polygons_iterator)
+                       {
+                               QJsonArray polygon = (*polygons_iterator).toArray();
+                               routes_from_polygon_coordinates(polygon);
+                       }
+               }
+               else if (geometry_type == MULTILINESTRING)
+               {
+                       QJsonArray line_strings = geometry.value(COORDINATES).toArray();
+                       for (auto lineStringIterator = line_strings.begin(); lineStringIterator != line_strings.end(); ++lineStringIterator)
+                       {
+                               QJsonArray coordinates = (*lineStringIterator).toArray();
+                               auto route = route_head_alloc();
+                               track_add_head(route);
+                               for (auto coordinates_iterator = coordinates.begin(); coordinates_iterator != coordinates.end(); ++coordinates_iterator)
+                               {
+                                       auto waypoint = waypoint_from_coordinates((*coordinates_iterator).toArray());
+                                       route_add_wpt(route, waypoint);
+                               }
+                       }
+               }
+       }
 }
 
+
 static void geojson_track_hdr(const route_head* track) {
   track_object = new QJsonObject();
 
-  (*track_object)[QStringLiteral("type")] = QStringLiteral("Feature");
+  (*track_object)[TYPE] = FEATURE;
   track_coords = new QJsonArray();
 
   QJsonObject properties;
   if (!track->rte_name.isEmpty()) {
-    properties["name"] = track->rte_name;
+    properties[NAME] = track->rte_name;
   }
-  (*track_object)["properties"] = properties;
+  (*track_object)[PROPERTIES] = properties;
 }
 
 static void geojson_track_disp(const Waypoint* trackpoint) {
@@ -141,9 +295,9 @@ static void geojson_track_disp(const Waypoint* trackpoint) {
 
 static void geojson_track_tlr(const route_head* track) {
   QJsonObject geometry;
-  geometry[QStringLiteral("type")] = QStringLiteral("LineString");
-  geometry[QStringLiteral("coordinates")] = *track_coords;
-  (*track_object)[QStringLiteral("geometry")] = geometry;
+  geometry[TYPE] = LINESTRING;
+  geometry[COORDINATES] = *track_coords;
+  (*track_object)[GEOMETRY] = geometry;
   feature_collection->append(*track_object);
   delete track_object;
   track_object = NULL;
index 29a2680c573aa718734c96e2fe847c9189a733ba..d68fe6db27ed19a79844f0d930593fe3844ce9af 100644 (file)
 
 #define MYNAME "mtk_logger"
 
+#ifdef __WIN32__
+#include <io.h>
+#define ftruncate _chsize
+#endif
+
 /* MTK packet id's -- currently unused... */
 enum MTK_NMEA_PACKET {
   PMTK_TEST = 0,
index b32d69b3af633157c9b453022370858d8a15429c..37dd85ba7b2e5e93a998e2ba52991f04c9790cc6 100644 (file)
@@ -72,11 +72,11 @@ tef_start(xg_string args, const QXmlStreamAttributes* attrv)
   bool valid = false;
 
   foreach(QXmlStreamAttribute attr, *attrv) {
-    if (attr.name().compare("Comment", Qt::CaseInsensitive) == 0) {
-      if (attr.value().compare("TourExchangeFormat", Qt::CaseInsensitive) == 0) {
+    if (attr.name().compare(QString("Comment"), Qt::CaseInsensitive) == 0) {
+      if (attr.value().compare(QString("TourExchangeFormat"), Qt::CaseInsensitive) == 0) {
         valid = true;
       }
-    } else if (attr.name().compare("Version", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("Version"), Qt::CaseInsensitive) == 0) {
       version = attr.value().toString().toDouble();
     }
   }
@@ -95,9 +95,9 @@ tef_header(xg_string args, const QXmlStreamAttributes* attrv)
 {
   route = route_head_alloc();
   foreach(QXmlStreamAttribute attr, *attrv) {
-    if (attr.name().compare("Name", Qt::CaseInsensitive) == 0) {
+    if (attr.name().compare(QString("Name"), Qt::CaseInsensitive) == 0) {
       route->rte_name = attr.value().toString().trimmed();
-    } else if (attr.name().compare("Software", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("Software"), Qt::CaseInsensitive) == 0) {
       route->rte_desc = attr.value().toString().trimmed();
     }
   }
@@ -248,20 +248,20 @@ tef_item_start(xg_string args, const QXmlStreamAttributes* attrv)
     QString attrstr = attr.value().toString();
     QByteArray attrtext = attrstr.toUtf8();
 
-    if (attr.name().compare("SegDescription", Qt::CaseInsensitive) == 0) {
+    if (attr.name().compare(QString("SegDescription"), Qt::CaseInsensitive) == 0) {
       wpt_tmp->shortname = attrstr.trimmed();
-    } else if (attr.name().compare("PointDescription", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("PointDescription"), Qt::CaseInsensitive) == 0) {
       wpt_tmp->description = attrstr.trimmed();
-    } else if (attr.name().compare("ViaStation", Qt::CaseInsensitive) == 0 &&
-               attr.value().compare("true", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("ViaStation"), Qt::CaseInsensitive) == 0 &&
+               attr.value().compare(QString("true"), Qt::CaseInsensitive) == 0) {
       wpt_tmp->wpt_flags.fmt_use = 1;  /* only a flag */
 
       /* new in TEF V2 */
-    } else if (attr.name().compare("Instruction", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("Instruction"), Qt::CaseInsensitive) == 0) {
       wpt_tmp->description = attrstr.trimmed();
-    } else if (attr.name().compare("Altitude", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("Altitude"), Qt::CaseInsensitive) == 0) {
       wpt_tmp->altitude = attrstr.toDouble();
-    } else if (attr.name().compare("TimeStamp", Qt::CaseInsensitive) == 0) {
+    } else if (attr.name().compare(QString("TimeStamp"), Qt::CaseInsensitive) == 0) {
       /* nothing for the moment */
     }
   }